package cn.com.sd.system.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Proxy;

import cn.com.sd.common.util.MyStringUtils;

@Entity
@Table(name="role")
@Proxy(lazy = false)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role implements java.io.Serializable {

	private static final long serialVersionUID = -6602142569593381312L;
	
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	private Integer id;
	
	@Column(name = "name")
	private String name;
	
	@Column(name = "status")
	private Integer status;
	
	@Column(name = "create_time")
	private Long createTime;
	
	@Column(name = "remark")
	private String remark;
	
	@ManyToMany(targetEntity=Operator.class, mappedBy = "roles",fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Operator> operators;
	
	//, cascade = {CascadeType.REMOVE, CascadeType.REFRESH}如果配了这个，就会也删除privilege表的相应数据了
	@ManyToMany(targetEntity = Privilege.class, fetch = FetchType.LAZY)
	@JoinTable(name = "role_privilege", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "priv_id"))
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Privilege> privileges;
	
	@SuppressWarnings("unused")
	@Transient
	private String privilegeIds;
	
	public void setPrivilegeIds(String privilegeIds) {
		this.privilegeIds = privilegeIds;
		
		if (StringUtils.isEmpty(privilegeIds)) {
			return;
		}
		Set<Privilege> priSet = new HashSet<Privilege>();
		String[] ids = privilegeIds.split(",");
		for (String str : ids) {
			if (StringUtils.isEmpty(str) || MyStringUtils.translateStringToInteger(str) == -1) {
				continue;
			}
			Privilege p = new Privilege();
			p.setId(MyStringUtils.translateStringToInteger(str));
			priSet.add(p);
		}
		this.privileges = priSet;
	}
	
	public Set<Privilege> getPrivileges() {
		return privileges;
	}
	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Long getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Long createTime) {
		this.createTime = createTime;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Set<Operator> getOperators() {
		return operators;
	}
	public void setOperators(Set<Operator> operators) {
		this.operators = operators;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
